package da.metricscraft;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.java.JavaPlugin;

import com.ubempire.map.HeightMap;
import com.ubempire.map.HeightMapGenerator;
import com.ubempire.map.MetaPopulator;

import da.metricscraft.information.MovementInformation.MoveActions;
import da.metricscraft.information.PlayerInformation;
import da.metricscraft.information.PlayerInformation.FrequencyData;
import da.metricscraft.information.PlayerInformation.PlayerDataType;
import da.metricscraft.listeners.MetricsBlockListener;
import da.metricscraft.listeners.MetricsEntityListener;
import da.metricscraft.listeners.MetricsPlayerListener;
import da.metricscraft.listeners.MetricsWorldListener;
import da.metricscraft.threads.InformationGatherer;
import da.metricscraft.threads.MovementHandler;
import da.metricscraft.threads.MovementHandler.MovementStruct;
import da.metricscraft.tools.MetricsCraftGenerator;
public class MetricsCraft extends JavaPlugin{
	public final HashMap<Player, ArrayList<Block>> mcUsers = new HashMap<Player, ArrayList<Block>>();
	public final HashMap<Player, Integer> mcPlayerLifetime = new HashMap<Player, Integer>();
	public final HashMap<Player, PlayerInformation> mcPlayerData  = new HashMap<Player, PlayerInformation>();
	private boolean defaultWorld;
	private ChunkGenerator generator;
	public static final Logger log = Logger.getLogger("Minecraft");
	public static InformationGatherer infoGatherer;
	public void onDisable() {
		log.info("MetricsCraft DISABLED");
	}

	
	public void onEnable() {
		registerEvents();
		infoGatherer = new InformationGatherer();
		infoGatherer.start();
		log.info("MetricsCraft ENABLED");
	}

	public PlayerInformation getPlayerInfo(Player p){
		return mcPlayerData.get(p);
	}
	public void setPlayerData(Player p, PlayerDataType type, int value){
		if (type.getMoveAction() != MoveActions.NONE){
			MovementStruct ms = new MovementStruct(p, p.getTicksLived() + mcPlayerLifetime.get(p), p.getLocation().toVector(), type.getMoveAction());
			MovementHandler.getInstance().addMovement(ms);
		}
		mcPlayerData.get(p).setData(type, value);
	}
	public void addPlayerData(Player p, PlayerDataType type, int value){
		if (type.getMoveAction() != MoveActions.NONE){
			if (type.getMoveAction() == MoveActions.TAKE_DAMAGE){
				for (int i = 0; i < value; i++){
					MovementStruct ms = new MovementStruct(p, p.getTicksLived() + mcPlayerLifetime.get(p), p.getLocation().toVector(), type.getMoveAction());
					MovementHandler.getInstance().addMovement(ms);
				}
			}else{
				MovementStruct ms = new MovementStruct(p, p.getTicksLived() + mcPlayerLifetime.get(p), p.getLocation().toVector(), type.getMoveAction());
				MovementHandler.getInstance().addMovement(ms);
			}
		}
		mcPlayerData.get(p).addData(type, value);
	}
	public void addPlayerData(FrequencyData fq){
		int data = fq.calculate();
		if (fq.getPlayer() != null)
			mcPlayerData.get(fq.getPlayer()).addData(fq.getDataType(), data);
	}
	private void registerEvents() {
		new MetricsBlockListener(this);
		new MetricsWorldListener(this);
		new MetricsPlayerListener(this);
		new MetricsEntityListener(this);
	}
	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
		if(commandLabel.equalsIgnoreCase("MetricsCraft")){
			if (args.length == 0){
				sender.sendMessage("Syntax of MetricsCraft is \"/metricscraft [playername]\"");
				return false;
			}else if (this.getServer().getPlayer(args[0]) == null){
				sender.sendMessage("Player " + args[0] + " not found!");
				return false;
			}else{
				log.info("Toggling MetricsCraft for " + args[0]);
				toggleMetricsCraft(this.getServer().getPlayer(args[0]));
				return true;
			}
		}else if(commandLabel.equalsIgnoreCase("kill")){
			
			this.getServer().getPlayer(args[0]).damage(9999);
			return true;
		}
		return false;
	}
	public void toggleMetricsCraft(Player player){
		if(enabled(player)){
			this.mcUsers.remove(player);
			player.sendMessage("MetricsCraft disabled for " + player.getDisplayName());
		}else{
			this.mcUsers.put(player, null);
			player.sendMessage("MetricsCraft enabled for " + player.getDisplayName());
		}
	}
	public boolean enabled(Player player){
		return this.mcUsers.containsKey(player);
		
	}


}
